package com.zh.mediaservice.controller.User;

import com.zh.mediacommon.result.PageResult;
import com.zh.mediacommon.result.Result;
import com.zh.mediapojo.dto.HospitalPageDto;
import com.zh.mediapojo.entity.Doctor;
import com.zh.mediapojo.entity.Hospital;
import com.zh.mediapojo.vo.HospitalPageVo;
import com.zh.mediapojo.vo.HospitalVo;
import com.zh.mediaservice.service.HospitalService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hospital")
@RequiredArgsConstructor
public class HospitalController {
    private static final Logger log = LoggerFactory.getLogger(HospitalController.class);
    private final HospitalService hospitalService;
    /*
    * 分页获取医院列表
    * @param hospitalPageDto
    * @return PageResult
    * */
    @GetMapping
    @Cacheable(value = "hospital",key = "#province+'_'+#nature+'_'+#level+'_'+#hclass+'_'+'_'+#page+'_'+#pageSize")
    public Result<PageResult> getHospitalPage(@RequestParam(name ="page", defaultValue = "1" ) Integer page,
                                              @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize,
                                              @RequestParam(name =  "province",required = false) String province,
                                              @RequestParam(name = "nature",required = false) String nature,
                                              @RequestParam(name = "level",required = false) String level,
                                              @RequestParam(name = "hclass",required = false) String hclass
                                              ){
//        if (hospitalPageDto==null)
//            hospitalPageDto = new HospitalPageDto(1,10,null,null,null,null);
        HospitalPageDto hospitalPageDto = new HospitalPageDto(page,pageSize,province,nature,level,hclass);
        log.info("获取医院列表{}",hospitalPageDto);
        PageResult pageResult =  hospitalService.page(hospitalPageDto);
        return Result.success(pageResult);
    }
    /*
    * 返回医院详细数据
    * @param id
    * */
    @GetMapping("/{id}")
    @Cacheable(value = "hospital_detailed",key = "#id")
    public Result<HospitalVo> getHospitalById(@PathVariable Integer id){
        HospitalVo hospital = hospitalService.getHospitalById(id);
        return Result.success(hospital);
    }
    /*
    * 搜索获取医院分页列表
    * @param page
    * @param pageSize
    * @param keyWords
    * @return PageResult
    * */
    @GetMapping("/search/{page}/{pageSize}/{keyWords}")
    @Cacheable(value = "hospital_search",key = "#page+'_'+#pageSize+'_'+#keyWords")
    public Result<PageResult> searchHospital(@PathVariable Integer page, @PathVariable Integer pageSize, @PathVariable String keyWords){
        PageResult hospitals = hospitalService.searchHospital(page, pageSize, keyWords);
        return Result.success(hospitals);
    }
    /*
    * 根据科室列表获取医生列表
    * @param sid
    * @return List<Doctor>
    * */
    @GetMapping("/doctor/{sid}")
    @Cacheable(value = "hospital_doctor",key = "#sid")
    public Result<List<Doctor>>getDoctorList(@PathVariable Integer sid){
       List<Doctor> doctors = hospitalService.getDoctorBySid(sid);
       return Result.success(doctors);
    }

    /*
    * 预约挂号接口
    * @param
    * @return
    * */
    @PostMapping("/reservation")
    public Result doReservation(){
        return Result.success();
    }

}
